package cn.xqj.basic.controller;

import cn.xqj.basic.dto.SmsDto;
import cn.xqj.basic.exception.BusinessException;
import cn.xqj.basic.service.IVerifyCodeService;
import cn.xqj.basic.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/captcha")
public class VerifyCodeController {

    @Autowired
    private IVerifyCodeService verifyCodeService;

    @GetMapping("/image")
    public AjaxResult captcha() {

        return verifyCodeService.captcha();


    }


    @PostMapping("/smsCode")
    public AjaxResult smsCode(@RequestBody SmsDto smsDto) {
        try {
            verifyCodeService.smsCode(smsDto);
            return AjaxResult.me().setMsg("验证码已发送！请在5分钟内使用");
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("服务器繁忙，请稍后再试！");
        }

    }

    @PostMapping("/binderSmsCode")
    public AjaxResult binderSmsCode(@RequestBody SmsDto smsDto) {
        try {
            verifyCodeService.binderSmsCode(smsDto);
            return AjaxResult.me().setMsg("验证码已发送！请在5分钟内使用");
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("服务器繁忙，请稍后再试！");
        }

    }
}
